package top.airprogram.user.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import top.airprogram.user.dao.StudentMapper;
import top.airprogram.user.dao.TeacherMapper;
import top.airprogram.user.entity.Student;
import top.airprogram.user.entity.Teacher;
import top.airprogram.user.service.UserService;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * Created by Riven on 2017/7/20.
 */

@Controller
//@RequestMapping("user")
public class UserController {

/*
    @RequestMapping("/test")
    public ModelAndView testUser() {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("user");
        UserBean user = new UserBean();
        user.setName("萌萌");
        user.setId(666);
        mav.addObject("user", user);
        return mav;
    }
*/


/*
@RequestMapping("/test/{id}/{name}")从jsp页面拿参数
    public String testUser(ModelMap modelMap, @PathVariable int id,@PathVariable String name){
    System.out.println(id);
    System.out.println(name);

    UserBean user = new UserBean();
        user.setId(47);
        user.setName("瓜萌萌");
    modelMap.addAttribute("user",user);
    return "user";
//    return "redirect:/demo";      redirect重定向
    }
*/

/*@RequestMapping("/test")//利用HttpServletRequest传递参数
    public String testUser(ModelMap modelMap, HttpServletRequest request){
    String id = request.getParameter("id");
    String name = request.getParameter("name");


    UserBean user = new UserBean();
        user.setId(47);
        user.setName("瓜萌萌");
    modelMap.addAttribute("user",user);
    return "user";
//    return "redirect:/demo";      redirect重定向
    }*/

  /*  @RequestMapping("/test")
    public String testUser(ModelMap modelMap, UserBean user){

        modelMap.addAttribute("user",user);
        return "user";
//    return "redirect:/demo";      redirect重定向
    }*/
//@Autowired
//  private StudentMapper studentMapper;

    @RequestMapping("/test")
    @ResponseBody//返回体，调用xml里的json方法
//    @Transactional
    public List<Student> testUser() {
//        UserBean user = new UserBean();
//        user.setId(666);
//        user.setName("萌萌!!!!");
        List<Student> list = studentMapper.selectAll();
        for(Student stu:list){
            System.out.println(stu);
        }
        return list;
    }




    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private UserService userService;


    @RequestMapping("/demo")
    public String demoUser() {
        Student student = new Student("小武", "人妖", 18, 2);
        Teacher teacher = new Teacher(2, "萌萌", "女");
        userService.insertStudentAndTeacher(student, teacher);
        return "demo";
    }

    @RequestMapping("upload")
    public String upload(MultipartFile file, HttpServletRequest request) throws IOException {
        String name = file.getOriginalFilename();
        File upload = new File("/" + name);
        file.transferTo(upload);

        return "demo";
    }
}
